#include "options.h"
#include "ui_options.h"

#include "mainwindow.h"

#include <QtXml/QXmlDefaultHandler>

QString OPTIONS_FILENAME = "Options.xml";

DFoptions dfOptions;

class OptionsParser : public QXmlDefaultHandler
{
public:

    bool startElement( const QString&, const QString&, const QString &name, const QXmlAttributes &attrs )
    {
        if ( name == "loadSettingsFileAtStartup" )
        {
            for( int i = 0; i < attrs.count(); i++ )
            {
                if( attrs.localName(i) == "enabled" )
                    dfOptions.loadSettingsFileAtStartup = attrs.value(i).toUpper()=="TRUE";
                else if( attrs.localName( i ) == "filename" )
                    dfOptions.settingsFilename = attrs.value(i);
            }
            return true;
        }
        return false;
    }

    bool fatalError(const QXmlParseException &exception)
    {
        qDebug() << "Parse error at line " << exception.lineNumber() << " column" << exception.columnNumber() << " " <<
                exception.message();
        return false;
    }

};


Options::Options(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Options)
{
    ui->setupUi(this);

    ui_checkSettingsFile = qFindChild<QCheckBox*>(this, "checkSettingsFile");
    ui_settingsFilename = qFindChild<QLineEdit*>(this, "settingsFilename");
    ui_buttonBrowseSettingsFilename = qFindChild<QPushButton*>(this,"buttonBrowseSettingsFilename");

    dfOptions.loadSettingsFileAtStartup = false;
    dfOptions.settingsFilename = "";

    if (QFile::exists(OPTIONS_FILENAME))
    {
        QFile optionsFile(OPTIONS_FILENAME);
        OptionsParser handler;
        QXmlInputSource source(&optionsFile);
        QXmlSimpleReader reader;
        reader.setContentHandler( &handler );
        reader.setErrorHandler( &handler );
        reader.parse( source );
    }

    if (dfOptions.loadSettingsFileAtStartup)
    {
        ui_checkSettingsFile->setChecked(true);
        ui_settingsFilename->setEnabled(true);
        ui_buttonBrowseSettingsFilename->setEnabled(true);
    }
    else
    {
        ui_checkSettingsFile->setChecked(false);
        ui_settingsFilename->setEnabled(false);
        ui_buttonBrowseSettingsFilename->setEnabled(false);
    }
    ui_settingsFilename->setText(dfOptions.settingsFilename);
}


Options::~Options()
{
    delete ui;
}


void Options::on_checkSettingsFile_stateChanged()
{
    if (ui_checkSettingsFile->isChecked())
    {
        ui_settingsFilename->setEnabled(true);
        ui_buttonBrowseSettingsFilename->setEnabled(true);
        dfOptions.loadSettingsFileAtStartup = true;
    }
    else
    {
        ui_settingsFilename->setEnabled(false);
        ui_buttonBrowseSettingsFilename->setEnabled(false);
        dfOptions.loadSettingsFileAtStartup = false;
    }
}


void Options::on_settingsFilename_editingFinished()
{
    dfOptions.settingsFilename = ui_settingsFilename->text();
}


void Options::on_buttonBrowseSettingsFilename_clicked()
{
    QString fileName;

    if (QFile::exists(dfOptions.settingsFilename))
        fileName = QFileDialog::getOpenFileName(this, "Foreman Settings", dfOptions.settingsFilename, "Foreman Settings (*.fml)");
    else
        fileName = QFileDialog::getOpenFileName(this, "Options File", QDir::currentPath()%QDir::separator()%".", "Foreman Settings (*.fml)");

    if (!fileName.isEmpty())
    {
        dfOptions.settingsFilename = fileName;
        ui_settingsFilename->setText(dfOptions.settingsFilename);
    }
}


void Options::accept()
{
    if (ui_checkSettingsFile->isChecked());

    QFile outputFile(OPTIONS_FILENAME);
    if (!outputFile.open(QIODevice::WriteOnly))
    {
        QMessageBox msgBox;
        msgBox.setText("Failed to save options file.");
        msgBox.exec();
        return;
    }

    QXmlStreamWriter writer(&outputFile);
    writer.setAutoFormatting(true);
    writer.writeStartDocument();

    writer.writeStartElement("loadSettingsFileAtStartup");
    writer.writeAttribute("enabled", dfOptions.loadSettingsFileAtStartup ? "true" : "false");
    writer.writeAttribute("filename", dfOptions.settingsFilename);
    writer.writeEndElement();

    writer.writeEndDocument();

    QDialog::done(0);
}


bool Options::getLoadSettingsFileAtStartup()
{
    return dfOptions.loadSettingsFileAtStartup;
}


QString Options::getSettingsFilename()
{
    return dfOptions.settingsFilename;
}
